﻿namespace Leetcode.N0135;
public class Solution
{
    public int Candy(int[] ratings)
    {
        int resultCount = 1;

        // 前一次的数量
        int preCount = 1;

        // 最后一次递增的数量
        int lastIncCount = 1;

        // 递减的次数
        int decCount = 0;

        for (int i = 1; i < ratings.Length; i++)
        {

            if (ratings[i] < ratings[i - 1])
            {
                decCount++;
                if (decCount == lastIncCount)
                {
                    decCount++;
                }

                preCount = 1;
                resultCount += decCount;
            }
            else if (ratings[i] > ratings[i - 1])
            {
                preCount++;
                resultCount += preCount;
                lastIncCount = preCount;
                decCount = 0;
            }
            else
            {
                preCount = 1;
                resultCount += preCount;
                lastIncCount = preCount;
                decCount = 0;
            }
        }

        return resultCount;
    }
}
